package com.greensoftcodeplus.sqllog.confingure;



import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;

import com.greensoftcodeplus.sqllog.component.MybatisLogInterceptor;
import com.greensoftcodeplus.sqllog.properties.MySqlPropertiesLog;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@AutoConfigureBefore(PageHelperAutoConfiguration.class)
@AutoConfigureAfter( MybatisAutoConfiguration.class)
@ComponentScan("com.greensoftcodeplus.sqllog")
public class MybatisLogPluginAutoConfingure implements InitializingBean {


    @Autowired
   ApplicationContext applicationContext;
   @Autowired
   private List<SqlSessionFactory> sqlSessionFactoryList;

   public MybatisLogPluginAutoConfingure(){
       System.out.println("MybatisLogPluginAutoConfingure");
   }

    @Override
   public void afterPropertiesSet()  {
       MybatisLogInterceptor mySqlLogInterceptor = new MybatisLogInterceptor(applicationContext.getBean(MySqlPropertiesLog.class));
       for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
           org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
           configuration.addInterceptor(mySqlLogInterceptor);
       }
   }
}
